package com.xiaozhengkeji.springboot.speed.api;


import com.xiaozhengkeji.springboot.speed.pojo.VerifyCode;
import com.xiaozhengkeji.springboot.speed.services.IVerifyCodeGen;
import com.xiaozhengkeji.springboot.speed.services.imp.SimpleCharVerifyCodeGenImpl;
import com.xiaozhengkeji.springboot.speed.utils.CookieUtil;
import com.xiaozhengkeji.springboot.speed.utils.EncryptUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RestController
@ApiOperation("验证码模块")
@RequestMapping("/verifyCode")
public class verifyCodeApi {


    @ApiOperation(value = "验证码获取")
    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response) {
        IVerifyCodeGen iVerifyCodeGen = new SimpleCharVerifyCodeGenImpl();
        try {
            //设置长宽
            VerifyCode verifyCode = iVerifyCodeGen.generate(80, 28);
            String code = verifyCode.getCode();
            //将VerifyCode绑定cookie
            Cookie cookie = new Cookie("VerifyCode", EncryptUtil.MD5加密(code));
            cookie.setPath("/");
            response.addCookie(cookie);
            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
            //设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            response.getOutputStream().flush();
        } catch (IOException e) {

        }
    }

    //    @ApiOperation(value = "验证码验证")
////    @GetMapping("/is")
////    public Boolean is(@RequestParam("verifyCode") String verifyCode, HttpServletRequest request, HttpServletResponse response) {
//////        for (Cookie cookie: request.getCookies()) {
//////            log.info(cookie.getName() + "---" + cookie.getValue());
//////        }
////        Cookie cookie = CookieUtil.getCookie(request.getCookies(), "VerifyCode");
////        if (cookie != null) {
////            if (EncryptUtil.MD5加密(verifyCode).equalsIgnoreCase(cookie.getValue())) {
////                return true;
////            } else {
////                return false;
////            }
////        } else {
////            return false;
////        }
////    }
    @ApiOperation(value = "验证码验证")
    @GetMapping("/is")
    public Boolean is(@RequestParam("verifyCode") String verifyCode, @RequestParam("verifyCodeS") String verifyCodeS) {

        if (EncryptUtil.MD5加密(verifyCode).equalsIgnoreCase(verifyCodeS)) {
            return true;
        } else {
            return false;
        }

    }
}
